//
//  SYCollectionTopView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/11.
//  Copyright © 2019年 李文强. All rights reserved.
//

#import "SYCollectionTopView.h"
#import "SYUploadView.h"


@interface SYCollectionTopView () <SYUploadViewDelegate,UITextFieldDelegate>
@property (strong, nonatomic) UITextField *textField;
@property (strong, nonatomic) UIButton *addFileBtn;//添加文件
@property (strong, nonatomic) UIButton *sortingBtn;//排序
@property (strong, nonatomic) UIView *fgxView;
@property (strong, nonatomic) UIButton *shangChuanBtn;//上传
@property (strong, nonatomic) SYUploadView *uploadView;//上传
@property (strong, nonatomic) UIButton *LuPingtton;

@property (strong, nonatomic) UIButton *HuaBanBtn;//画板

@end



@implementation SYCollectionTopView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initSubView];
        [self initLayout];
    }
    return self;
}

- (void)initSubView {
    [self addSubview:self.textField];
    [self addSubview:self.addFileBtn];
    [self addSubview:self.sortingBtn];
    [self addSubview:self.fgxView];
    [self addSubview:self.shangChuanBtn];
    [self addSubview:self.HuaBanBtn];

//    [self addSubview:self.LuPingtton];

}

- (void)initLayout {
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.mas_left).offset(160);
//        make.right.equalTo(self.mas_right).offset(-160);
        make.centerX.equalTo(self.mas_centerX);
        make.height.mas_equalTo(30);
        make.top.equalTo(self.mas_top).offset(5);
        make.width.mas_equalTo(400);
    }];
    
    [self.addFileBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(20);
        make.centerY.equalTo(self.mas_centerY);
        make.height.with.mas_equalTo(40);
    }];
    
    [self.shangChuanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.addFileBtn.mas_right).offset(20);
        make.centerY.equalTo(self.mas_centerY);
        make.height.with.mas_equalTo(40);
    }];
    [self.HuaBanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shangChuanBtn.mas_right).offset(20);
        make.centerY.equalTo(self.mas_centerY);
        make.height.with.mas_equalTo(40);
    }];
    
    [self.sortingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-20);
        make.centerY.equalTo(self.mas_centerY);
        make.height.with.mas_equalTo(40);
    }];
//    [self.LuPingtton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.sortingBtn.mas_right).offset(-20);
//        make.centerY.equalTo(self.mas_centerY);
//        make.height.mas_equalTo(40);
//        make.width.mas_equalTo(80);
//    }];
    [self.fgxView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self);
        make.height.mas_equalTo(1);
    }];
}

-(void)kaiqiLuPing{
    if (self.kaiQiLuPingBlock) {
        self.kaiQiLuPingBlock();
    }
}
- (void)setFunction:(SYFunctoinModel *)function {
    _function = function;
    if ([function.titleName containsString:@"我的收藏"] || [function.titleName containsString:@"教研文件"]) {
        if ([function.titleName containsString:@"我的收藏"]) {
            if (self.isChar == YES) {//群聊辅导
                self.textField.hidden = YES;
                self.addFileBtn.hidden = YES;
                self.sortingBtn.hidden = YES;
            }
            else {
                self.textField.hidden = YES;
            }
        }
        self.shangChuanBtn.hidden = YES;
        self.HuaBanBtn.hidden = YES;

    } else if ([function.titleName containsString:@"我的作品"]) {
        if (self.isChar == YES) {
            self.textField.hidden = YES;
            self.addFileBtn.hidden = YES;
            self.shangChuanBtn.hidden = YES;
            self.sortingBtn.hidden = YES;
            self.HuaBanBtn.hidden = YES;

        } else {
            self.shangChuanBtn.hidden = YES;
            self.HuaBanBtn.hidden = YES;

        }
        self.textField.hidden = YES;  //搜索框
    } else if ([function.titleName containsString:@"机构文件"]) {
        self.shangChuanBtn.hidden = YES;
        self.addFileBtn.hidden = YES;
        self.textField.hidden = YES;
        self.HuaBanBtn.hidden = YES;

    } else if ([function.titleName containsString:@"微课"]) { //zy_weke_zzwk
        self.shangChuanBtn.hidden = NO;//
        self.HuaBanBtn.hidden = NO;

        self.textField.hidden = YES;
        [self.addFileBtn setImage:[UIImage imageNamed:@"zy_weke_zzwk"] forState:UIControlStateNormal];
    }
    if (self.isfrom.length > 0) {
        self.textField.hidden = YES;
        self.addFileBtn.hidden = YES;
        self.shangChuanBtn.hidden = YES;
        self.sortingBtn.hidden = YES;
        self.HuaBanBtn.hidden = YES;

    }
}

#pragma mark - set
- (void)addFileBtnClick:(UIButton *)send {
    SYCollectionTopType type = 0;
    NSInteger tag = send.tag;
    if (tag == 1001) {//排序 zy_collection_sc
        send.selected = !send.selected;
        if (send.selected) {
            type = SYCollectionTopTypeTab;
        } else {

            type = SYCollectionTopTypeCollection;
        }
    } else if (tag == 1002){//添加新文件
        type = SYCollectionTopTypeAdd;
    } else if (tag == 1003) {//上传
        type = SYCollectionTopTypeLuWeiKe;
//        _uploadView = [[SYUploadView alloc] init];
//        [_uploadView showUploadView:send uploadView:_uploadView];
//        _uploadView.delegate = self;
    }else if (tag == 1005) {//画板
            type = SYCollectionTopTypeHuaBan;
    //        _uploadView = [[SYUploadView alloc] init];
    //        [_uploadView showUploadView:send uploadView:_uploadView];
    //        _uploadView.delegate = self;
        }
    
    if ([self.delegate respondsToSelector:@selector(selectType:)]) {
        [self.delegate selectType:type];
    }
    
}
#pragma mark - uplview
- (void)syuploadViewType:(SYUploadViewType)type {
    SYCollectionTopType staunt = -1;
    switch (type) {
        case SYUploadViewTypePaiZhao://拍照
            staunt = SYCollectionTopTypePaiZhao;
            break;
        case SYUploadViewTypeZhaoPian://照片
            staunt = SYCollectionTopTypeZhaoPian;
            break;
        case SYUploadViewTypeLiuLan://浏览
            staunt = SYCollectionTopTypeLiuLan;
            break;
            
        default:
            break;
    }
    if ([self.delegate respondsToSelector:@selector(selectType:)]) {
        [self.delegate selectType:staunt];
    }
}


#pragma mark - set
- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zy_collection_ss"]];
        imageView.frame = CGRectMake(20, 5, 20, 20);
        _textField.leftView =  imageView;
        _textField.placeholder = @"搜索";
        _textField.returnKeyType = UIReturnKeySearch;
        _textField.leftViewMode = UITextFieldViewModeAlways;
        _textField.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        _textField.borderStyle = UITextBorderStyleRoundedRect;
        _textField.layer.masksToBounds = YES;
        _textField.layer.cornerRadius = 14;
        _textField.delegate = self;
        _textField.textAlignment = NSTextAlignmentCenter;
    }
    return _textField;
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    [_textField resignFirstResponder];
    if (self.searchBlock) {
        self.searchBlock(theTextField.text, self.function.titleName);
    }


    return YES;
}
- (UIButton *)addFileBtn {
    if (!_addFileBtn) {
        _addFileBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addFileBtn setImage:[UIImage imageNamed:@"zy_gg_add"]  forState:UIControlStateNormal];
        
        _addFileBtn.tag = 1002;
        [_addFileBtn addTarget:self action:@selector(addFileBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addFileBtn;
}
- (UIButton *)sortingBtn {
    if (!_sortingBtn) {
        _sortingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sortingBtn setImage:[UIImage imageNamed:@"zy_gg_px"]  forState:UIControlStateNormal];
        [_sortingBtn setImage:[UIImage imageNamed:@"zy_gg_lb"] forState:UIControlStateSelected];
        _sortingBtn.tag = 1001;
        [_sortingBtn addTarget:self action:@selector(addFileBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sortingBtn;
}
- (UIButton *)shangChuanBtn {
    if (!_shangChuanBtn) {
        _shangChuanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shangChuanBtn setImage:[UIImage imageNamed:@"zy_weike_lp"]  forState:UIControlStateNormal];
        
        _shangChuanBtn.tag = 1003;
        [_shangChuanBtn addTarget:self action:@selector(addFileBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _shangChuanBtn;
}
- (UIButton *)HuaBanBtn {
    if (!_HuaBanBtn) {
        _HuaBanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_HuaBanBtn setImage:[UIImage imageNamed:@"zy_weke_huaban"]  forState:UIControlStateNormal];
        
        _HuaBanBtn.tag = 1005;
        [_HuaBanBtn addTarget:self action:@selector(addFileBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _HuaBanBtn;
}
- (UIButton *)LuPingtton {
    if (!_LuPingtton) {
        _LuPingtton = [[UIButton alloc]init];
        _LuPingtton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_LuPingtton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        [_LuPingtton setTitle:@"录制微课" forState:UIControlStateNormal];
        [_LuPingtton addTarget:self action:@selector(kaiqiLuPing) forControlEvents:UIControlEventTouchUpInside];
    }
    return _LuPingtton;
}
- (UIView *)fgxView {
    if (!_fgxView) {
        _fgxView = [[UIView alloc] init];
        _fgxView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
    }
    return _fgxView;
}


@end
